package com.endes.android.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.endes.android.entidades.SSEC01;
import com.endes.android.model.database.EndesOpenHelper;

public class QSAntecedentesDao {
	private EndesOpenHelper endesOpenHelper;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	public QSAntecedentesDao(Context context) {
		this.context = context;
		endesOpenHelper = new EndesOpenHelper(context);

	}

	public SSEC01 ObtenerAntecedentesMEF(int qsCluster, int qsNumber, int qsHome, 
			int numeroOrdenPersonaSeleccionada, int mef) {
		SSEC01 ssec01 = null;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=? and qsMEF=?",
						new String[] { qsCluster + "", qsNumber + "", qsHome + "", 
								numeroOrdenPersonaSeleccionada + "", mef + "" });
		if (cursor.moveToFirst()) {
			ssec01 = new SSEC01();
			ssec01.setQsCluster(qsCluster);
			ssec01.setQsNumber(qsNumber);
			ssec01.setQsHome(qsHome);
			ssec01.setQs20(cursor.getString(cursor.getColumnIndex("qs20")));	
			ssec01.setQs20C(numeroOrdenPersonaSeleccionada);
			ssec01.setQsSexo(cursor.getInt(cursor.getColumnIndex("qsSexo")));
			ssec01.setQs21AH(cursor.getInt(cursor.getColumnIndex("qs21AH")));
			ssec01.setQs21AM(cursor.getInt(cursor.getColumnIndex("qs21AM")));
			ssec01.setQsMEF(mef);
			if(mef == 2){
				ssec01.setQs22M(cursor.getInt(cursor.getColumnIndex("qs22M")));
				ssec01.setQs22A(cursor.getInt(cursor.getColumnIndex("qs22A")));
				ssec01.setQs23(cursor.getInt(cursor.getColumnIndex("qs23")));
				ssec01.setQs24(cursor.getInt(cursor.getColumnIndex("qs24")));
				if(cursor.isNull(cursor.getColumnIndex("qs25N"))){
					ssec01.setQs25N(null);
				} else{
					ssec01.setQs25N(cursor.getInt(cursor.getColumnIndex("qs25N")));
				}
				if(cursor.isNull(cursor.getColumnIndex("qs25AG"))){
					ssec01.setQs25AG(null);
				} else{
					ssec01.setQs25AG(cursor.getInt(cursor.getColumnIndex("qs25AG")));
				}
				if(cursor.isNull(cursor.getColumnIndex("qs25A"))){
					ssec01.setQs25A(null);
				} else{
					ssec01.setQs25A(cursor.getInt(cursor.getColumnIndex("qs25A")));
				}
				if(cursor.isNull(cursor.getColumnIndex("qs25G"))){
					ssec01.setQs25G(null);
				} else{
					ssec01.setQs25G(cursor.getInt(cursor.getColumnIndex("qs25G")));
				}
				ssec01.setQs26(cursor.getInt(cursor.getColumnIndex("qs26")));
				if(cursor.isNull(cursor.getColumnIndex("qs27"))){
					ssec01.setQs27(null);
				} else{
					ssec01.setQs27(cursor.getString(cursor.getColumnIndex("qs27")));
				}
				if(cursor.isNull(cursor.getColumnIndex("qs27X"))){
					ssec01.setQs27X(null);
				} else{
					ssec01.setQs27X(cursor.getString(cursor.getColumnIndex("qs27X")));
				}
			} else{
				ssec01.setQs23(cursor.getInt(cursor.getColumnIndex("qs23")));
				ssec01.setQs28(cursor.getInt(cursor.getColumnIndex("qs28")));
				ssec01.setQs29A(cursor.getInt(cursor.getColumnIndex("qs29A")));
				ssec01.setQs29B(cursor.getInt(cursor.getColumnIndex("qs29B")));
			}
		}
		Cerrar();
		return ssec01;
	}

	public void guardarAntecedentesEncuesta(SSEC01 ssec01, int mef) {
		ContentValues contenido = new ContentValues();
		contenido.put("qs20", ssec01.getQs20());
		contenido.put("qsSexo", ssec01.getQsSexo());
		contenido.put("qs21AH", ssec01.getQs21AH());
		contenido.put("qs21AM", ssec01.getQs21AM());
		contenido.put("qsMEF", ssec01.getQsMEF());
		if(mef == 2){
			contenido.put("qs22M", ssec01.getQs22M());
			contenido.put("qs22A", ssec01.getQs22A());
			contenido.put("qs23", ssec01.getQs23());
			contenido.put("qs24", ssec01.getQs24());
			contenido.put("qs25N", ssec01.getQs25N());
			contenido.put("qs25AG", ssec01.getQs25AG());
			contenido.put("qs25A", ssec01.getQs25A());
			contenido.put("qs25G", ssec01.getQs25G());
			contenido.put("qs26", ssec01.getQs26());
			contenido.put("qs27", ssec01.getQs27());
			contenido.put("qs27X", ssec01.getQs27X());
		} else{
			contenido.put("qs23", ssec01.getQs23());
			contenido.put("qs28", ssec01.getQs28());
			contenido.put("qs29A", ssec01.getQs29A());
			contenido.put("qs29B", ssec01.getQs29B());
		}
		
		if (ExisteSSEC01(ssec01)) {
			db = endesOpenHelper.getWritableDatabase();
			db.update(
					"SSEC01",
					contenido,
					"qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
					new String[] { ssec01.getQsCluster() + "",
							ssec01.getQsNumber() + "", ssec01.getQsHome() + "",
							ssec01.getQs20C() + ""});
			db.close();
		} else {
			db = endesOpenHelper.getWritableDatabase();
			contenido.put("qsCluster", ssec01.getQsCluster());
			contenido.put("qsNumber", ssec01.getQsNumber());
			contenido.put("qsHome", ssec01.getQsHome());
			contenido.put("qs20C", ssec01.getQs20C());
			db.insert("SSEC01", null, contenido);
			db.close();
		}
	}

	public boolean ExisteSSEC01(SSEC01 ssec01) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? " +
						"and qs20C=?",
						new String[] { ssec01.getQsCluster() + "",
								ssec01.getQsNumber() + "",
								ssec01.getQsHome() + "",
								ssec01.getQs20C() + ""});
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}
	
	public int sexoPersonaSeleccionada(int qsCluster, int qsNumber, int qsHome, int personaSeleccionada){
		int id = 0;
		int sexo = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select id from HOUSEHOLD where QHCLUSTER=? and QHNUMBER=? and QHHOME=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + ""});
		if (cursor.moveToFirst()) {
			id = cursor.getInt(cursor.getColumnIndex("id"));
		}
		cursor = db
				.rawQuery(
						"Select QHSEXO from HOUSEHOLD_SEC02 where form_id=? and QHORDEN=?",
						new String[] { id + "", personaSeleccionada + ""});
		if (cursor.moveToFirst()) {
			sexo = cursor.getInt(cursor.getColumnIndex("QHSEXO"));
		}
		return sexo;
	}
	
	public int edadPersonaSeleccionada(int qsCluster, int qsNumber, int qsHome, int personaSeleccionada){
		int id = 0;
		int edad = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select id from HOUSEHOLD where QHCLUSTER=? and QHNUMBER=? and QHHOME=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + ""});
		if (cursor.moveToFirst()) {
			id = cursor.getInt(cursor.getColumnIndex("id"));
		}
		cursor = db
				.rawQuery(
						"Select QHEDAD from HOUSEHOLD_SEC02 where form_id=? and QHORDEN=?",
						new String[] { id + "", personaSeleccionada + ""});
		if (cursor.moveToFirst()) {
			edad = cursor.getInt(cursor.getColumnIndex("QHEDAD"));
		}
		return edad;
	}

	public void Cerrar() {
		cursor.close();
		db.close();
	}

}
